/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.actor;

import java.util.ArrayList;
import java.util.List;

import org.openaecfm.model.actor.Address;
import org.openaecfm.model.actor.PostalAddress;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class PostalAddressIO extends
		AbstractEntityInstanceInputImpl<PostalAddress> implements
		IEntityInstanceOutput<PostalAddress> {

	private IEntityInstanceUtilInput<Address> utilInput;
	private IEntityInstanceUtilOutput<Address> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Address> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(PostalAddress pojo, IAppDataManager appDataManager) {

		SimpleEntityInstance ei = new SimpleEntityInstance("IFCPOSTALADDRESS",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set InternalLocation
		if (pojo.getInternalLocation() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getInternalLocation(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set AddressLines
		if (pojo.getAddressLines() == null || pojo.getAddressLines().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (String str : pojo.getAddressLines()) {
				StringParameter s = new StringParameter(str,
						StringParameter.UTF_8);
				l.add(s);
			}
			ei.addParameter(l);
		}

		// Set PostalBox
		if (pojo.getPostalBox() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getPostalBox(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set Town
		if (pojo.getTown() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getTown(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set Region
		if (pojo.getRegion() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getRegion(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set PostalCode
		if (pojo.getPostalCode() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getPostalCode(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set Country
		if (pojo.getCountry() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getCountry(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected PostalAddress doCreateObject() {
		return new PostalAddress();
	}

	public void setUtilInput(IEntityInstanceUtilInput<Address> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			PostalAddress pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set InternalLocation
		Parameter internal = sei.getParameter(3);
		if (internal.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) internal;
			pojo.setInternalLocation(s.toUTF8());
		}

		// Set AddressLines
		Parameter addlines = sei.getParameter(4);
		if (addlines.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) addlines;
			List<String> addressLines = new ArrayList<String>();
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;

				addressLines.add(s.toUTF8());
			}
			pojo.setAddressLines(addressLines);
		}

		// Set PostalBox
		Parameter postalbox = sei.getParameter(5);
		if (postalbox.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) postalbox;
			pojo.setPostalBox(s.toUTF8());
		}

		// Set Town
		Parameter town = sei.getParameter(6);
		if (town.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) town;
			pojo.setTown(s.toUTF8());
		}

		// Set Region
		Parameter region = sei.getParameter(7);
		if (region.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) region;
			pojo.setRegion(s.toUTF8());
		}

		// Set PostalCode
		Parameter postalcode = sei.getParameter(8);
		if (postalcode.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) postalcode;
			pojo.setPostalCode(s.toUTF8());
		}

		// Set Country
		Parameter country = sei.getParameter(9);
		if (country.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) country;
			pojo.setCountry(s.toUTF8());
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			PostalAddress pojo, IAppDataManager appDataManager) {
		return ;
	}
}
